home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / fileutil / fileutils-3.16.tar.gz / fileutils-3.16.tar / fileutils-3.16 / intl / finddomain.c < prev    next >
C/C++ Source or Header  |  1996-11-27  |  6KB  |  218 lines

  1. /* finddomain.c -- handle list of needed message catalogs
  2.    Copyright (C) 1995, 1996 Free Software Foundation, Inc.
  3.    Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  18.  
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22.  
  23. #include <ctype.h>
  24. #include <errno.h>
  25. #include <stdio.h>
  26. #include <sys/types.h>
  27.  
  28. #if defined STDC_HEADERS || defined _LIBC
  29. # include <stdlib.h>
  30. #else
  31. # ifdef HAVE_MALLOC_H
  32. #  include <malloc.h>
  33. # else
  34. void free ();
  35. # endif
  36. #endif
  37.  
  38. #if defined HAVE_STRING_H || defined _LIBC
  39. # include <string.h>
  40. #else
  41. # include <strings.h>
  42. # ifndef strchr
  43. #  define strchr index
  44. # endif
  45. # ifndef memcpy
  46. #  define memcpy(Dst, Src, Num) bcopy (Src, Dst, Num)
  47. # endif
  48. #endif
  49.  
  50. #if defined HAVE_UNISTD_H || defined _LIBC
  51. # include <unistd.h>
  52. #endif
  53.  
  54. #include "gettext.h"
  55. #include "gettextP.h"
  56. #ifdef _LIBC
  57. # include <libintl.h>
  58. #else
  59. # include "libgettext.h"
  60. #endif
  61.  
  62. /* @@ end of prolog @@ */
  63.  
  64. #ifdef _LIBC
  65. /* Rename the non ANSI C functions.  This is required by the standard
  66.    because some ANSI C functions will require linking with this object
  67.    file and the name space must not be polluted.  */
  68. # define stpcpy(dest, src) __stpcpy(dest, src)
  69. #else
  70. # ifndef HAVE_STPCPY
  71. static char *stpcpy PARAMS ((char *dest, const char *src));
  72. # endif
  73. #endif
  74.  
  75. /* List of already loaded domains.  */
  76. static struct loaded_l10nfile *_nl_loaded_domains;
  77.  
  78.  
  79. /* Return a data structure describing the message catalog described by
  80.    the DOMAINNAME and CATEGORY parameters with respect to the currently
  81.    established bindings.  */
  82. struct loaded_l10nfile *
  83. _nl_find_domain (dirname, locale, domainname)
  84.      const char *dirname;
  85.      char *locale;
  86.      const char *domainname;
  87. {
  88.   struct loaded_l10nfile *retval;
  89.   const char *language;
  90.   const char *modifier;
  91.   const char *territory;
  92.   const char *codeset;
  93.   const char *normalized_codeset;
  94.   const char *special;
  95.   const char *sponsor;
  96.   const char *revision;
  97.   const char *alias_value;
  98.   int mask;
  99.  
  100.   /* LOCALE can consist of up to four recognized parts for the XPG syntax:
  101.  
  102.         language[_territory[.codeset]][@modifier]
  103.  
  104.      and six parts for the CEN syntax:
  105.  
  106.     language[_territory][+audience][+special][,sponsor][_revision]
  107.  
  108.      Beside the first all of them are allowed to be missing.  If the
  109.      full specified locale is not found, the less specific one are
  110.      looked for.  The various part will be stripped of according to
  111.      the following order:
  112.         (1) revision
  113.         (2) sponsor
  114.         (3) special
  115.         (4) codeset
  116.         (5) normalized codeset
  117.         (6) territory
  118.         (7) audience/modifier
  119.    */
  120.  
  121.   /* If we have already tested for this locale entry there has to
  122.      be one data set in the list of loaded domains.  */
  123.   retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
  124.                    strlen (dirname) + 1, 0, locale, NULL, NULL,
  125.                    NULL, NULL, NULL, NULL, NULL, domainname, 0);
  126.   if (retval != NULL)
  127.     {
  128.       /* We know something about this locale.  */
  129.       int cnt;
  130.  
  131.       if (retval->decided == 0)
  132.     _nl_load_domain (retval);
  133.  
  134.       if (retval->data != NULL)
  135.     return retval;
  136.  
  137.       for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
  138.     {
  139.       if (retval->successor[cnt]->decided == 0)
  140.         _nl_load_domain (retval->successor[cnt]);
  141.  
  142.       if (retval->successor[cnt]->data != NULL)
  143.         break;
  144.     }
  145.       return cnt >= 0 ? retval : NULL;
  146.       /* NOTREACHED */
  147.     }
  148.  
  149.   /* See whether the locale value is an alias.  If yes its value
  150.      *overwrites* the alias name.  No test for the original value is
  151.      done.  */
  152.   alias_value = _nl_expand_alias (locale);
  153.   if (alias_value != NULL)
  154.     {
  155.       size_t len = strlen (alias_value) + 1;
  156.       locale = (char *) malloc (len);
  157.       if (locale == NULL)
  158.     return NULL;
  159.  
  160.       memcpy (locale, alias_value, len);
  161.     }
  162.  
  163.   /* Now we determine the single parts of the locale name.  First
  164.      look for the language.  Termination symbols are `_' and `@' if
  165.      we use XPG4 style, and `_', `+', and `,' if we use CEN syntax.  */
  166.   mask = _nl_explode_name (locale, &language, &modifier, &territory,
  167.                &codeset, &normalized_codeset, &special,
  168.                &sponsor, &revision);
  169.  
  170.   /* Create all possible locale entries which might be interested in
  171.      generalzation.  */
  172.   retval = _nl_make_l10nflist (&_nl_loaded_domains, dirname,
  173.                    strlen (dirname) + 1, mask, language, territory,
  174.                    codeset, normalized_codeset, modifier, special,
  175.                    sponsor, revision, domainname, 1);
  176.   if (retval == NULL)
  177.     /* This means we are out of core.  */
  178.     return NULL;
  179.  
  180.   if (retval->decided == 0)
  181.     _nl_load_domain (retval);
  182.   if (retval->data == NULL)
  183.     {
  184.       int cnt;
  185.       for (cnt = 0; retval->successor[cnt] != NULL; ++cnt)
  186.     {
  187.       if (retval->successor[cnt]->decided == 0)
  188.         _nl_load_domain (retval->successor[cnt]);
  189.       if (retval->successor[cnt]->data != NULL)
  190.         break;
  191.     }
  192.     }
  193.  
  194.   /* The room for an alias was dynamically allocated.  Free it now.  */
  195.   if (alias_value != NULL)
  196.     free (locale);
  197.  
  198.   return retval;
  199. }
  200.  
  201. /* @@ begin of epilog @@ */
  202.  
  203. /* We don't want libintl.a to depend on any other library.  So we
  204.    avoid the non-standard function stpcpy.  In GNU C Library this
  205.    function is available, though.  Also allow the symbol HAVE_STPCPY
  206.    to be defined.  */
  207. #if !_LIBC && !HAVE_STPCPY
  208. static char *
  209. stpcpy (dest, src)
  210.      char *dest;
  211.      const char *src;
  212. {
  213.   while ((*dest++ = *src++) != '\0')
  214.     /* Do nothing. */ ;
  215.   return dest - 1;
  216. }
  217. #endif
  218.